home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / textool / sunstuff.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  38KB  |  1,621 lines

  1. /*
  2.  * This file contains the Sun specific bits of TeXtool, a TeX previewer
  3.  * derived from the texview posted on the network about spring 86, which
  4.  * in turn was derived from an earlier version and so on right back to
  5.  * a version for the BBN Bitgraph.
  6.  *
  7.  * This stuff deals with all SunView related things and calls things
  8.  * in the dvistuff file to do the DVI file and font file stuff, which
  9.  * in turn calls things in here like SetChar and SetRule to drive the
  10.  * screen.
  11.  */
  12.  
  13. /*
  14.  * Version 2.1 of 30 Oct 87 -- first real outside release
  15.  *
  16.  * -> V2.2, 27 Nov 87 -- fixed bug where .dvi file was opened twice,
  17.  *                        once from FrameDoneProc() and once by the WIN_REPAINT
  18.  *                        case in CanvasEventProc(). This ran us out of fds.
  19.  * -> V2.3, 29 Mar 88 -- fixed sloppy code in skip_specials() in
  20. pkstuff.c
  21.   *                                            that caused specials
  22. to be missed and treated as chars.
  23.  
  24.  */
  25.  
  26. /* include the various global #defines */
  27. #include "defs.h"
  28.  
  29. /* now some definitions for the menu items */
  30. /*
  31.  * NOTE: The menu items must be in the positions in the menu given by
  32.  * these values. This is not done automatically!
  33.  */
  34. #define M_NULL 0
  35. #define M_RESTART 1
  36. #define M_NEWPAGE 2
  37. #define M_NEWFILE 3
  38. #define M_CD 4
  39. #define M_TEX 5
  40. #define M_LATEX 6
  41. #define M_EDIT 7
  42. #define M_SHELL 8
  43. #define SM_TOGGLE_P 0x11
  44. #define SM_TOGGLE_BIGP 0x12
  45. #define SM_TOGGLE_B 0x13
  46. #define SM_TOGGLE_M 0x14
  47. #define DM_TOGGLE_D 0x21
  48. #define DM_TOGGLE_X 0x22
  49. #define DM_SHOW_FD 0x23
  50. #define DM_SHOW_PXL_CACHE 0x24
  51.  
  52. /* now some Sun include files */
  53. #include <sys/types.h>
  54. #include <sys/stat.h>
  55. #include <sys/file.h>
  56. #include <stdio.h>
  57. #include <pwd.h>
  58. #include <pixrect/pixrect_hs.h>
  59. #include <suntool/sunview.h>
  60. #include <suntool/icon_load.h>
  61. #include <suntool/canvas.h>
  62. #include <suntool/scrollbar.h>
  63. #include <suntool/panel.h>
  64. #include <suntool/tty.h>
  65.  
  66. /* general external procedures */
  67. char *getenv();
  68. char *index();
  69. char *rindex();
  70. char *sprintf();
  71. char *strcpy();
  72. char *strcat();
  73. int strlen();
  74. char *getwd();
  75.  
  76. /* procedures from dvistuff */
  77. extern void        CloseDviFile();
  78. extern void        DoPage();
  79. extern Boolean    DviFileChanged();
  80. extern void        FreeFontStorage();
  81. extern void        OpenDviFile();
  82. #ifdef DEBUG
  83. extern void        ShowPxlFd();
  84. extern void        ShowPxlCache();
  85. #endif
  86.  
  87. /* procedures from pxlstuff or pkstuff */
  88. extern void        LoadAChar();
  89.  
  90. /* procedures in this file */
  91. void        BatchEnd();
  92. void        BatchStart();
  93. void        CanvasEventProc();
  94. Notify_value CanvasInterposer();
  95. void        CheckFiles();
  96. void        ErrorEventProc();
  97. char        *ExpandTilde();
  98. void        Fatal();
  99. void        FrameDoneProc();
  100. void        InitWindows();
  101. void        MenuCd();
  102. void        MenuEdit();
  103. Menu        MenuGenerate();
  104. void        MenuLatex();
  105. void        MenuNewFile();
  106. void        MenuNewPage();
  107. void        MenuRestart();
  108. void        MenuShell();
  109. void        MenuShortPage();
  110. void        MenuTex();
  111. void        MenuToggleFlag();
  112. void        ProcessPage();
  113. void        RestartDviFile();
  114. void        SetBop();
  115. void        SetChar();
  116. void        SetFrameLabel();
  117. void        SetPageMenu();
  118. void        SetRule();
  119. Menu        ShiftMenuGenerate();
  120. void        SplitFilename();
  121. void        TextInputProc();
  122. void        Warning();
  123. #ifdef DEBUG
  124. Menu        DebugMenuGenerate();
  125. void        ShowFd();
  126. #endif
  127.  
  128. /* now include real global variables needed by both files */
  129. #define _DEFINE
  130. #include "globals.h"
  131.  
  132. /* now the "globals" only needed here */
  133.  
  134. static char progname[STRSIZE];            /* program name */
  135. static char ttyinbuf[STRSIZE];            /* for input to ttysubwindow */
  136. static char dirbuf[STRSIZE];            /* current working dir. */
  137. static char framelabelbuf[STRSIZE];        /* for frame label */
  138. static char curarea[STRSIZE];            /* current file area */
  139. static char curext[STRSIZE];            /* current file extension */
  140. static char curname[STRSIZE];            /* current file name */
  141. static char textinputbuffer[STRSIZE];    /* for copying text panel item value */
  142. static char dviname[STRSIZE];            /* current complete DVI filename */
  143. static short textinput_state;
  144.  
  145. /* display related stuff */
  146.  
  147. static int hh;                        /* current horizontal position in pixels */
  148. static int vv;                        /* current vertical position in pixels */
  149. static int current_page;            /* pagepointers index for current page */
  150.  
  151. static int xcanvasoffset = RESOLUTION;    /* 1 inch offset to match TeX's */
  152. static int ycanvasoffset = RESOLUTION;    /* 1 inch margin on paper */
  153.  
  154. static Boolean keyscroll;            /* bodge to say h,j,k or l typed */
  155.  
  156. /* SunView defs */
  157. Frame frame;
  158. Icon icon;
  159. Canvas canvas;
  160. Pixwin *pw;
  161. Scrollbar vsb, hsb;
  162. Cursor canvas_cursor;
  163. Menu menu, tenpagemenu, shiftmenu;
  164. Menu_item shortpageitem, longpageitem;
  165. Frame ttyframe;
  166. Tty ttysubwindow;
  167. Frame errorframe;
  168. Panel errorpanel;
  169. Panel_item errorheader, errormessage;
  170. Panel_item textinput;
  171. #ifdef DEBUG
  172. Menu debugmenu;
  173. #endif
  174.  
  175. /* now the real code */
  176.  
  177. main(argc, argv)
  178. int argc;
  179. char *argv[];
  180.  
  181. {
  182.  
  183.     extern int optind;
  184.     extern char *optarg;
  185.     int c;
  186.  
  187.     strcpy(progname, argv[0]);
  188.  
  189.                                 /* initialse some globals */
  190.     Debug = FALSE;
  191.     ExtraDebug = FALSE;
  192.     PreLoad = FALSE;
  193.     BigPreLoad = FALSE;
  194.     Batching = TRUE;
  195.     main_state = SB_INITIAL;
  196.     Mflag = FALSE;
  197.     pagepointers = NULL;        /* we haven't malloc'd them yet */
  198.  
  199.     InitWindows(&argc, argv);        /* setup windows and consume -W args */
  200.  
  201.     current_page = 0;                /* start at the beginning */
  202.  
  203.    /*  get enviroment variables for different PXL directories */
  204.  
  205.    if ((PXLpath=getenv("PXLPATH")) == NULL)
  206.         PXLpath = PXLFONTAREA;
  207.    if ((PKpath=getenv("PKPATH")) == NULL)
  208.         PKpath = PKFONTAREA;
  209.  
  210.     while ((c = getopt(argc, argv, "dxpPbm:")) != EOF)
  211.             switch(c) {
  212.                 case 'd':        /* d selects Debug output */
  213.                     Debug = TRUE;
  214.                     break;
  215.  
  216.                 case 'x':        /* extra debug info for pxl files */
  217.                     ExtraDebug = TRUE;
  218.                     break;
  219.  
  220.                 case 'p':        /* p enables font pre-loading */
  221.                     PreLoad = TRUE;
  222.                     break;
  223.  
  224.                 case 'P':        /* P enables char pixrect pre-loading */
  225.                     BigPreLoad = TRUE;
  226.                     break;
  227.  
  228.                 case 'b':        /* turn batching off for slowness */
  229.                     Batching = FALSE;
  230.                     break;
  231.  
  232.                 case 'm':        /* specify mag to override that in DVI file */
  233.                     Mflag = TRUE;
  234.                     user_mag = atoi(optarg);
  235.                     break;
  236.  
  237.                 case '?':
  238.                 default:
  239.                     fprintf(stderr,
  240.                         "usage: textool [-pPb] [-m <mag>] [+n] file\n");
  241.                     exit(1);
  242.             }
  243.  
  244.     if ((optind < argc) && (argv[optind][0] == '+'))
  245.                                         /* we have starting page number */
  246.         sscanf(&argv[optind][1], "%d", ¤t_page);
  247.  
  248.     if (optind >= argc) {
  249.         main_state = SB_NOFILE;                /* cancel SB_INITIAL status */
  250.         SplitFilename("<no_file>");        /* _ as icon labels don't like space */
  251.     }
  252.     else
  253.         SplitFilename(argv[optind]);
  254.     SetFrameLabel();
  255.  
  256. #ifdef DEBUG
  257.     if (Debug) fprintf(stderr,"going into window_main_loop()\n");
  258. #endif
  259.     window_main_loop(frame);
  260. }
  261.  
  262.  
  263. /*-->InitWindows*/
  264.  
  265. void
  266. InitWindows(argcptr, argv)        /* initialize SunView stuff */
  267. int *argcptr;
  268. char **argv;
  269.  
  270. {
  271. Pixrect *pr1;
  272. char error[IL_ERRORMSG_SIZE];
  273. int i;
  274.  
  275.     pr1 = icon_load_mpr("/u/tex/textool/textool.icon", error); 
  276.     if (pr1 == NULL)                /* couldn't load icon */
  277.         fprintf(stderr, "%s\n", error);
  278.                                 /* not Fatal() as windows are not yet active */
  279.     icon = icon_create(ICON_IMAGE, pr1,
  280.                         0);
  281.  
  282.     frame = window_create(0,FRAME,
  283.                         WIN_WIDTH, 900,
  284.                         WIN_HEIGHT, 900,
  285.                         WIN_X, 100,
  286.                         WIN_Y, 0,
  287.                         WIN_SHOW, FALSE,
  288.                         FRAME_LABEL, "TeXtool",
  289.                         FRAME_ICON, icon,
  290.                         FRAME_ARGC_PTR_ARGV, argcptr, argv,
  291.                         0);
  292.  
  293.     vsb = scrollbar_create(SCROLL_LINE_HEIGHT, 20,
  294.                         SCROLL_ADVANCED_MODE, TRUE,
  295.                         0);
  296.     hsb = scrollbar_create(SCROLL_LINE_HEIGHT, 20,
  297.                         SCROLL_ADVANCED_MODE, TRUE,
  298.                         0);
  299.  
  300.     canvas = window_create(frame,CANVAS,
  301.                          CANVAS_AUTO_SHRINK, FALSE,
  302.                          CANVAS_AUTO_EXPAND, FALSE,
  303.                          CANVAS_WIDTH, (17 * RESOLUTION)/2, /* 8.5in */
  304.                          CANVAS_HEIGHT, 12 * RESOLUTION, /* 12in */
  305.                          WIN_VERTICAL_SCROLLBAR, vsb,
  306.                          WIN_HORIZONTAL_SCROLLBAR, hsb,
  307.                          WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS,
  308.                          WIN_EVENT_PROC, CanvasEventProc,
  309.                          0);
  310.     pw = canvas_pixwin(canvas);
  311.  
  312.                                 /* interpose before canvas for pageing */
  313.     (void)notify_interpose_event_func(canvas, CanvasInterposer, NOTIFY_SAFE);
  314.  
  315.     scrollbar_scroll_to(hsb, (long)RESOLUTION/2);
  316.     scrollbar_scroll_to(vsb, (long)RESOLUTION);
  317.  
  318.                                 /* now make the menu for canvas */
  319.     tenpagemenu = menu_create(MENU_ACTION_PROC, MenuShortPage, 0);
  320.     menu_set(tenpagemenu,
  321.                 MENU_STRING_ITEM, "1", 1,
  322.                 MENU_STRING_ITEM, "2", 2,
  323.                 MENU_STRING_ITEM, "3", 3,
  324.                 MENU_STRING_ITEM, "4", 4,
  325.                 MENU_STRING_ITEM, "5", 5,
  326.                 MENU_STRING_ITEM, "6", 6,
  327.                 MENU_STRING_ITEM, "7", 7,
  328.                 MENU_STRING_ITEM, "8", 8,
  329.                 MENU_STRING_ITEM, "9", 9,
  330.                 MENU_STRING_ITEM, "10", 10,
  331.                 0);
  332.  
  333.     shortpageitem = menu_create_item(MENU_PULLRIGHT_ITEM, "go to page",
  334.                                 tenpagemenu, 0);
  335.     longpageitem = menu_create_item(MENU_ACTION_ITEM, "go to page",
  336.                                 MenuNewPage, 0);
  337.  
  338.     menu = menu_create(MENU_GEN_PROC, MenuGenerate,
  339.                         MENU_ITEM,
  340.                         MENU_ACTION_ITEM, "restart DVI file", MenuRestart,
  341.                         0,
  342.                         MENU_ITEM,
  343.                         MENU_ACTION_ITEM, "new file", MenuNewFile,
  344.                         0,
  345.                         MENU_ITEM,
  346.                         MENU_ACTION_ITEM, "change directory", MenuCd,
  347.                         0,
  348.                         MENU_ITEM,
  349.                         MENU_ACTION_ITEM, "run tex", MenuTex,
  350.                         0,
  351.                         MENU_ITEM,
  352.                         MENU_ACTION_ITEM, "run latex", MenuLatex,
  353.                         0,
  354.                         MENU_ITEM,
  355.                         MENU_ACTION_ITEM, "edit .tex file", MenuEdit,
  356.                         0,
  357.                         MENU_ITEM,
  358.                         MENU_ACTION_ITEM, "shell", MenuShell,
  359.                         0,0);
  360.     menu_set(menu, MENU_INSERT, 1, shortpageitem, 0);
  361.  
  362.                                 /* now a second menu for shift-right button */
  363.     shiftmenu = menu_create(MENU_GEN_PROC, ShiftMenuGenerate,
  364.                         MENU_ITEM,
  365.                         MENU_ACTION_PROC, MenuToggleFlag,
  366.                         MENU_VALUE, SM_TOGGLE_P,
  367.                         0,
  368.                         MENU_ITEM,
  369.                         MENU_ACTION_PROC, MenuToggleFlag,
  370.                         MENU_VALUE, SM_TOGGLE_BIGP,
  371.                         0,
  372.                         MENU_ITEM,
  373.                         MENU_ACTION_PROC, MenuToggleFlag,
  374.                         MENU_VALUE, SM_TOGGLE_B,
  375.                         0,
  376.                         MENU_ITEM,
  377.                         MENU_ACTION_PROC, MenuToggleFlag,
  378.                         MENU_VALUE, SM_TOGGLE_M,
  379.                         0,0);
  380.  
  381. #ifdef DEBUG
  382.                                 /* now a debug menu for the meta key */
  383.     debugmenu = menu_create(MENU_GEN_PROC, DebugMenuGenerate,
  384.                         MENU_ITEM,
  385.                         MENU_ACTION_PROC, MenuToggleFlag,
  386.                         MENU_VALUE, DM_TOGGLE_D,
  387.                         0,
  388.                         MENU_ITEM,
  389.                         MENU_ACTION_PROC, MenuToggleFlag,
  390.                         MENU_VALUE, DM_TOGGLE_X,
  391.                         0,
  392.                         MENU_ITEM,
  393.                         MENU_ACTION_ITEM, "show fd", ShowFd,
  394.                         MENU_VALUE, DM_SHOW_FD,
  395.                         0,
  396.                         MENU_ITEM,
  397.                         MENU_ACTION_ITEM, "show PXL cache", ShowPxlCache,
  398.                         MENU_VALUE, DM_SHOW_PXL_CACHE,
  399.                         0,0);
  400. #endif DEBUG
  401.  
  402.                                 /* now create a frame and panel for errors */
  403.  
  404.     errorframe = window_create(frame, FRAME,
  405.                                 FRAME_NO_CONFIRM, TRUE,
  406.                                 WIN_SHOW, FALSE,
  407.                                 WIN_COLUMNS, 80,
  408.                                 WIN_ROWS, 2,
  409.                                 WIN_X, 100,
  410.                                 WIN_Y, 100,
  411.                                 0);
  412.     errorpanel = window_create(errorframe, PANEL,
  413.                                 PANEL_BACKGROUND_PROC, ErrorEventProc,
  414.                                 PANEL_EVENT_PROC, ErrorEventProc,
  415.                                 0);
  416.     errorheader = panel_create_item(errorpanel, PANEL_MESSAGE,
  417.                                 PANEL_LABEL_STRING, "TeXtool Warning:",
  418.                                 0);
  419.     errormessage = panel_create_item(errorpanel, PANEL_MESSAGE,
  420.                                 PANEL_LABEL_STRING, "dummy message",
  421.                                 0);
  422.     textinput = panel_create_item(errorpanel, PANEL_TEXT,
  423.                                 PANEL_LABEL_STRING, "Magnification:",
  424.                                 PANEL_ITEM_Y, ATTR_ROW(1),
  425.                                 PANEL_ITEM_X, ATTR_COL(0),
  426.                                 PANEL_SHOW_ITEM, FALSE,
  427.                                 PANEL_VALUE_DISPLAY_LENGTH, 40,
  428.                                 PANEL_NOTIFY_PROC, TextInputProc,
  429.                                 0);
  430.     window_fit(errorpanel);
  431.     window_fit(errorframe);
  432.                                         /* create shell window for TeX, etc */
  433.     ttyframe = window_create(frame, FRAME,
  434.                                 FRAME_LABEL, "TeXtool Shell",
  435.                                 FRAME_SHOW_LABEL, TRUE,
  436.                                 FRAME_NO_CONFIRM, TRUE,
  437.                                 FRAME_DONE_PROC, FrameDoneProc,
  438.                                 WIN_X, 25,
  439.                                 WIN_Y, 25,
  440.                                 WIN_SHOW, FALSE,
  441.                                 0);
  442.     ttysubwindow = window_create(ttyframe, TTY,
  443.                                 0);
  444. }
  445.  
  446.  
  447. /*-->SetFrameLabel*/
  448.  
  449. void
  450. SetFrameLabel()
  451. {
  452.     if (pagepointers == NULL)
  453.         sprintf(framelabelbuf,
  454.             "TeXtool: Directory: %s File: %s%s%s",
  455.             getwd(dirbuf), curarea, curname, curext);
  456.     else
  457.         sprintf(framelabelbuf,
  458.             "TeXtool: Directory: %s File: %s%s%s \"Page\": %d \\count0: %d",
  459.             getwd(dirbuf), curarea, curname, curext,
  460.             current_page+1, pagepointers[current_page].count_0);
  461.  
  462.  
  463.     window_set(frame, FRAME_LABEL, framelabelbuf, 0);
  464. }
  465.  
  466.  
  467.                         /*
  468.                          * Now two procs to deal with canvas input.
  469.                          * CanvasInterposer is called first and filters out
  470.                          * the vertical scroll page requests, turning them into
  471.                          * calls of ProcessPage.
  472.                          * CanvasEventProc handles all other input cases.
  473.                          */
  474. /*-->CanvasInterposer*/
  475.  
  476. Notify_value
  477. CanvasInterposer(object, event, arg, type)
  478. Canvas object;
  479. Event *event;
  480. Notify_arg arg;
  481. Notify_event_type type;
  482. {
  483.     Scroll_motion motion;
  484.     Notify_value retval;
  485.  
  486. #ifdef DEBUG
  487.     if (Debug) fprintf(stderr,"Interposer called (%d) - ", event_id(event));
  488.     if (Debug) fprintf(stderr,
  489.                         "\nobject = %X, *event = %X, arg = %X, type = %X\n",
  490.                         object, event, arg, type);
  491. #endif
  492.     if (event_id(event) == SCROLL_REQUEST) {
  493.         if ((Scrollbar)arg == vsb) {
  494. #ifdef DEBUG
  495.             if (Debug) fprintf(stderr,"arg is vsb, ");
  496. #endif
  497.             motion = (Scroll_motion)scrollbar_get(vsb, SCROLL_REQUEST_MOTION);
  498.             if (!keyscroll && ((motion == SCROLL_PAGE_FORWARD)
  499.                                     || (motion == SCROLL_PAGE_BACKWARD))) {
  500. #ifdef DEBUG
  501.                 if (Debug) fprintf(stderr,"motion is %X (PAGE), ", (int)motion);
  502. #endif
  503.                                         /* now tell bar to undo scroll */
  504.                 scrollbar_set(vsb, SCROLL_VIEW_START,
  505.                                 scrollbar_get(vsb, SCROLL_LAST_VIEW_START), 0);
  506.                 if (motion == SCROLL_PAGE_BACKWARD) {
  507.                     if (current_page-- > 0)
  508.                         ProcessPage(current_page);
  509.                        else
  510.                         current_page++;
  511.                 }
  512.                 else {
  513.                     if (++current_page < totalpages)
  514.                         ProcessPage(current_page);
  515.                     else
  516.                         current_page--;
  517.                 }
  518. #ifdef DEBUG
  519.                 if (Debug) fprintf(stderr,"Interposer return (DONE)\n");
  520. #endif
  521.                 return (NOTIFY_DONE);
  522.             }
  523.         }
  524.     }
  525. #ifdef DEBUG
  526.     if (Debug) fprintf(stderr,"calling next func\n");
  527. #endif
  528.     retval = (notify_next_event_func(object, event, arg, type));
  529.     if (Debug) fprintf(stderr, "Interposer return (%X)\n", retval);
  530.     return (retval);
  531. }
  532.  
  533.  
  534. /*-->CanvasEventProc*/
  535.  
  536. void
  537. CanvasEventProc(notused, event)
  538. Canvas notused;
  539. Event *event;
  540. {
  541.     static int intval;
  542.  
  543. #ifdef DEBUG
  544.     if (Debug) fprintf(stderr,"event proc called (id = %d) - ",
  545.                         event_id(event));
  546. #endif
  547.  
  548.         keyscroll = FALSE;
  549.         switch (event_id(event)) {
  550.             case '\n':
  551.             case '\r':
  552.                 if (main_state & (SB_PLUSNUMBER | SB_MINUSNUMBER)) {
  553.                     if ((main_state & SB_MINUSNUMBER)
  554.                         && (current_page -= intval) < 0)
  555.                         current_page = 0;
  556.                     if ((main_state & SB_PLUSNUMBER)
  557.                         && (current_page += intval) >= totalpages)
  558.                         current_page = totalpages-1;
  559.                     main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  560.                     main_state |= SB_INTERPRET;
  561.                     ProcessPage(current_page);
  562.                     break;
  563.                 }                /* else fall into next page case */
  564.             case ' ':                /* normal case - start next page */
  565.             case 'n':
  566.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  567.                 main_state |= SB_INTERPRET;
  568.                 if (++current_page < totalpages)
  569.                     ProcessPage(current_page);
  570.                 else
  571.                     current_page--;
  572.                 break;
  573.  
  574.             case 'b':
  575.             case 'P':
  576.             case 'p':                /* redisplay from previous page */
  577.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  578.                 main_state |= SB_INTERPRET;
  579.                 if (current_page-- > 0)
  580.                     ProcessPage(current_page);
  581.                 else
  582.                     current_page++;
  583.                 break;
  584.  
  585. #ifdef KEYBOARDSCROLL
  586.             case 'j':
  587.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  588.                 main_state |= SB_INTERPRET;
  589.                 keyscroll = TRUE;
  590.                 scrollbar_scroll_to(vsb, (long)600);
  591.                 break;
  592.  
  593.             case 'h':
  594.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  595.                 main_state |= SB_INTERPRET;
  596.                 keyscroll = TRUE;
  597.                 scrollbar_scroll_to(hsb, (long)0);
  598.                 break;
  599.  
  600.             case 'l':
  601.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  602.                 main_state |= SB_INTERPRET;
  603.                 keyscroll = TRUE;
  604.                 scrollbar_scroll_to(hsb, (long)300);
  605.                 break;
  606.  
  607.             case 'k':
  608.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  609.                 main_state |= SB_INTERPRET;
  610.                 keyscroll = TRUE;
  611.                 scrollbar_scroll_to(vsb, (long)0);
  612.                 break;
  613.  
  614.             case 'c':
  615.                 main_state &= (~SB_PLUSNUMBER & ~SB_MINUSNUMBER);
  616.                 main_state |= SB_INTERPRET;
  617.                 keyscroll = TRUE;
  618.                 scrollbar_scroll_to(vsb, (long)RESOLUTION);
  619.                 scrollbar_scroll_to(hsb, (long)(RESOLUTION/2));
  620.                 break;
  621. #endif
  622.  
  623. #ifdef DEBUG
  624.             case SCROLL_REQUEST:        /* scroll request, after Sb gets it */
  625.                 { int a, b, c, d;
  626.                 if (Debug) {
  627.                     a = (int)scrollbar_get(vsb, SCROLL_VIEW_START);
  628.                     b = (int)scrollbar_get(vsb, SCROLL_LAST_VIEW_START);
  629.                     c = (int)scrollbar_get(hsb, SCROLL_VIEW_START);
  630.                     d = (int)scrollbar_get(hsb, SCROLL_LAST_VIEW_START);
  631.                     fprintf(stderr,"\nVsb scroll from %d to %d\n", b, a);
  632.                     fprintf(stderr,"Hsb scroll from %d to %d\n", d, c);
  633.                 }
  634.                 }
  635.                 break;
  636.  
  637. #endif
  638.             case '-':
  639.                 /* read in val */
  640.                 main_state |= SB_MINUSNUMBER;
  641.                 intval = 0;
  642.                 break;
  643.  
  644.             case '+':
  645.                 /* read in val */
  646.                 main_state |= SB_PLUSNUMBER;
  647.                 intval = 0;
  648.                 break;
  649.  
  650.             case '0':
  651.             case '1':
  652.             case '2':
  653.             case '3':
  654.             case '4':
  655.             case '5':
  656.             case '6':
  657.             case '7':
  658.             case '8':
  659.             case '9':
  660.                 if (main_state & (SB_PLUSNUMBER | SB_MINUSNUMBER))
  661.                     intval = intval*10 + (event_id(event) - '0');
  662.                 break;
  663.  
  664.             case '\b':
  665.             case 127:                /* delete key */
  666.                 if (main_state & (SB_PLUSNUMBER | SB_MINUSNUMBER))
  667.                     intval /= 10;
  668.                 break;
  669.  
  670.             case MS_RIGHT:
  671.                 if (event_is_up(event)) break;
  672. #ifdef DEBUG
  673.                 if (event_meta_is_down(event)) {
  674.                     (void)menu_show(debugmenu, canvas,
  675.                         canvas_window_event(canvas, event), 0);
  676.                     break;
  677.                 }
  678. #endif
  679.                 if (event_shift_is_down(event))
  680.                     (void)menu_show(shiftmenu, canvas,
  681.                         canvas_window_event(canvas, event), 0);
  682.                 else
  683.                     (void)menu_show(menu, canvas,
  684.                         canvas_window_event(canvas, event), 0);
  685.                 break;
  686.  
  687.             case MS_LEFT:
  688.                 canvas_cursor = window_get(canvas, WIN_CURSOR);
  689.                 if (event_is_up(event)) {
  690.                     cursor_set(canvas_cursor,
  691.                                 CURSOR_SHOW_CURSOR, TRUE,
  692.                                 CURSOR_SHOW_CROSSHAIRS, FALSE,
  693.                                 0);
  694.                 }
  695.                 else {
  696.                                         /*
  697.                                          * All these except the pixrect op
  698.                                          * are defaults but it does not seem
  699.                                          * to work unles they are specified
  700.                                          */
  701.                     cursor_set(canvas_cursor,
  702.                                 CURSOR_SHOW_CURSOR, FALSE,
  703.                                 CURSOR_CROSSHAIR_COLOR, 1,
  704.                                 CURSOR_CROSSHAIR_THICKNESS, 1,
  705.                                 CURSOR_CROSSHAIR_GAP, 0,
  706.                                 CURSOR_CROSSHAIR_LENGTH, CURSOR_TO_EDGE,
  707.                                 CURSOR_CROSSHAIR_OP, PIX_SRC|PIX_DST,
  708.                                 CURSOR_SHOW_CROSSHAIRS, TRUE,
  709.                                 0);
  710.                 }
  711.                 window_set(canvas, WIN_CURSOR, canvas_cursor, 0);
  712.                 break;
  713.  
  714.             case WIN_REPAINT:
  715.                 if (main_state & SB_INITIAL) { /* first time of display */
  716.                     main_state &= ~SB_INITIAL;
  717.                     CheckFiles();        /* sets main_state */
  718.                     OpenDviFile(dviname);
  719.                     if (main_state & (SB_NOFILE | SB_NODVIFILE))
  720.                         break;
  721.                     SetPageMenu();
  722.                     main_state &= ~SB_NEWFILE;
  723.                     main_state |= SB_INTERPRET;
  724.                     ProcessPage(current_page);
  725.                 }
  726.                 break;
  727.  
  728.             default:
  729. #ifdef DEBUG
  730.                 if (Debug) fprintf(stderr,"event proc default case - ");
  731. #endif
  732.         }
  733. #ifdef DEBUG
  734.     if (Debug) fprintf(stderr,"event proc return\n");
  735. #endif
  736. }
  737.  
  738.  
  739.                         /*
  740.                          * Now the procedures that actually do the work of
  741.                          * drawing on the screen. These get called from
  742.                          * dvistuff to prevent it having to know about
  743.                          * SunView.
  744.                          * SetBop just clears the screen and sets the initial
  745.                          * offset. SetChar and SetRule draw text and lines.
  746.                          */
  747. /*-->SetBop*/
  748.  
  749. void
  750. SetBop()
  751. {
  752.     pw_writebackground(pw, 0, 0,
  753.         pw->pw_prretained->pr_size.x, pw->pw_prretained->pr_size.y,
  754.         PIX_CLR);
  755.                         /*
  756.                          * Start with h and v at offset point rather than 0,0.
  757.                          * This means that offset does not need to be added in
  758.                          * on every call to SetChar and SetRule.
  759.                          */
  760.     h = xcanvasoffset * hconv;
  761.     v = ycanvasoffset * vconv;
  762. }
  763.  
  764.  
  765. /*-->SetChar*/
  766.  
  767. void
  768. SetChar(charptr, Set)
  769. struct char_entry *charptr;
  770. Boolean Set;
  771. {
  772.     hh = PixRound(h, hconv);
  773.     vv = PixRound(v, vconv);
  774.     if (!charptr->where.isloaded) LoadAChar(charptr);
  775.     if (charptr->where.isloaded == TRUE)
  776.         pw_rop(pw, hh-charptr->xOffset,
  777.             vv-charptr->yOffset,
  778.             charptr->width, charptr->height, PIX_SRC | PIX_DST,
  779.             charptr->where.address.pixrectptr, 0, 0);
  780.     if (Set)
  781.         h += charptr->tfmw;
  782. }
  783.  
  784.  
  785. /*-->SetRule*/
  786.  
  787. void
  788. SetRule(a, b, Set)
  789. int a, b;
  790. Boolean Set;
  791.  
  792. {            /*         this routine draws \hrule and \vrule */
  793.     int ehh, evv;
  794.  
  795.     hh = PixRound(h, hconv);
  796.     vv = PixRound(v-a, vconv);
  797.     ehh = PixRound(h + b, hconv);
  798.     evv = PixRound(v, vconv);
  799.     if (hh == ehh) ehh++;
  800.     if (vv == evv) vv--;
  801.     if ((a > 0) && (b > 0))
  802.             pw_rop(pw, hh, vv,
  803.                 ehh-hh, evv-vv, PIX_SET, NULL, 0, 0);
  804.     if (Set)
  805.         h += b;
  806. }
  807.  
  808.  
  809.                         /*
  810.                          * These next two are only procedures here to avoid
  811.                          * having dvistuff.c know about Pixwins.
  812.                          * As they are only called once per page the loss of
  813.                          * speed is not a problem.
  814.                          */
  815. /*-->BatchStart*/
  816.  
  817. void
  818. BatchStart()
  819. {
  820.     pw_batch_on(pw);
  821. }
  822.  
  823.  
  824. /*-->BatchEnd*/
  825.  
  826. void
  827. BatchEnd()
  828. {
  829.     pw_batch_off(pw);
  830. }
  831.  
  832.  
  833.                         /*
  834.                          * Now two procedures that call on things in
  835.                          * dvistuff to get pages displayed.
  836.                          */
  837. /*-->ProcessPage*/
  838.  
  839. void
  840. ProcessPage(page)
  841. int page;
  842. {
  843.     if (DviFileChanged())
  844.         RestartDviFile();
  845.         SetFrameLabel();
  846.     DoPage(page);
  847. }
  848.  
  849.  
  850. /*-->RestartDviFile*/
  851.  
  852. void
  853. RestartDviFile()
  854. {
  855.     CloseDviFile();
  856.     FreeFontStorage();
  857.     main_state &= (~SB_INTERPRET & ~SB_NOFILE);
  858. /*    main_state |= SB_NEWFILE;        /* to allow a try at open */
  859.     CheckFiles();                    /* sets main_state */
  860.     OpenDviFile(dviname);
  861.     if (main_state & (SB_NOFILE | SB_NODVIFILE)) /* something wrong */
  862.         return;
  863.     SetPageMenu();
  864.     main_state &= ~SB_NEWFILE;    /* SB_NEWFILE must be set to get to here */
  865.     main_state |= SB_INTERPRET;
  866. }
  867.  
  868.  
  869.                         /*
  870.                          * Now to the menu generating procedures, one for
  871.                          * each menu. There is also a procedure to set
  872.                          * the "go to page" menu item depending on the
  873.                          * number of pages.
  874.                          * MenuGenerate() deals with most of the interlocking
  875.                          * to avoid illegal menu choices.
  876.                          */
  877. /*-->MenuGenerate*/
  878.  
  879. Menu
  880. MenuGenerate(object, operation)
  881. Menu object;
  882. Menu_generate operation;
  883. {
  884.     if (operation == MENU_CREATE) {
  885.         menu_set(menu_get(object, MENU_NTH_ITEM, M_RESTART),
  886.                 MENU_INACTIVE,
  887.                 (main_state & (SB_TEXING | SB_NOFILE | SB_NODVIFILE ) ?
  888.                         TRUE : FALSE),
  889.                 0);
  890.         menu_set(menu_get(object, MENU_NTH_ITEM, M_NEWPAGE),
  891.                 MENU_INACTIVE,
  892.                 (main_state & (SB_TEXING | SB_NOFILE | SB_NODVIFILE) ?
  893.                         TRUE : FALSE),
  894.                 0);
  895.         menu_set(menu_get(object, MENU_NTH_ITEM, M_TEX),
  896.                 MENU_INACTIVE,
  897.                 (main_state & (SB_TEXING | SB_EDITING | SB_SHELL | SB_NOFILE |
  898.                                 SB_NOTEXFILE) ? TRUE : FALSE),
  899.                 0);
  900.         menu_set(menu_get(object, MENU_NTH_ITEM, M_LATEX),
  901.                 MENU_INACTIVE,
  902.                 (main_state & (SB_TEXING | SB_EDITING | SB_SHELL | SB_NOFILE |
  903.                                 SB_NOTEXFILE) ? TRUE : FALSE),
  904.                 0);
  905.         menu_set(menu_get(object, MENU_NTH_ITEM, M_EDIT),
  906.                 MENU_INACTIVE,
  907.                 (main_state & (SB_TEXING | SB_EDITING | SB_SHELL | SB_NOFILE) ?
  908.                                                         TRUE : FALSE),
  909.                 0);
  910.         menu_set(menu_get(object, MENU_NTH_ITEM, M_SHELL),
  911.                 MENU_INACTIVE,
  912.                 (main_state & (SB_TEXING | SB_EDITING | SB_SHELL) ?
  913.                                                         TRUE : FALSE),
  914.                 0);
  915.     }
  916.     return (object);
  917.  
  918. }
  919.  
  920.  
  921. /*-->ShiftMenuGenerate*/
  922.  
  923. Menu
  924. ShiftMenuGenerate(object, operation)
  925. Menu object;
  926. Menu_generate operation;
  927. {
  928.     if (operation == MENU_CREATE) {
  929.         menu_set(menu_get(object, MENU_NTH_ITEM, SM_TOGGLE_P & 0xF),
  930.                 MENU_STRING, (PreLoad ? "reset -p flag" : "set -p flag"),
  931.                 0);
  932.         menu_set(menu_get(object, MENU_NTH_ITEM, SM_TOGGLE_B & 0xF),
  933.                 MENU_STRING, (Batching ? "set -b flag" : "reset -b flag"),
  934.                 0);
  935.         menu_set(menu_get(object, MENU_NTH_ITEM, SM_TOGGLE_BIGP & 0xF),
  936.                 MENU_STRING, (BigPreLoad ? "reset -P flag" : "set -P flag"),
  937.                 0);
  938. #ifdef USEGLOBALMAG
  939.         menu_set(menu_get(object, MENU_NTH_ITEM, SM_TOGGLE_M & 0xF),
  940.                 MENU_STRING, (Mflag ? "reset -m flag" : "set -m flag"),
  941.                 0);
  942. #endif
  943.     }
  944.     return (object);
  945. }
  946.  
  947.  
  948. #ifdef DEBUG
  949. /*-->DebugMenuGenerate*/
  950.  
  951. Menu
  952. DebugMenuGenerate(object, operation)
  953. Menu object;
  954. Menu_generate operation;
  955. {
  956.     if (operation == MENU_CREATE) {
  957.         menu_set(menu_get(object, MENU_NTH_ITEM, DM_TOGGLE_D & 0xF),
  958.                 MENU_STRING, (Debug ? "reset -d flag" : "set -d flag"),
  959.                 0);
  960.         menu_set(menu_get(object, MENU_NTH_ITEM, DM_TOGGLE_X & 0xF),
  961.                 MENU_STRING, (ExtraDebug ? "reset -x flag" : "set -x flag"),
  962.                 0);
  963.     }
  964.     return (object);
  965. }
  966. #endif DEBUG
  967.  
  968.  
  969. /*-->SetPageMenu*/
  970.  
  971. void
  972. SetPageMenu()
  973. {
  974.     short i;
  975.  
  976.     if (totalpages > 10)
  977.         menu_set(menu,
  978.                 MENU_REPLACE, 2, longpageitem,
  979.                 0);
  980.     else {
  981.         menu_set(menu,
  982.                 MENU_REPLACE, 2, shortpageitem,
  983.                 0);
  984.         for (i = 0; i < 10; i++)
  985.             menu_set(menu_get(tenpagemenu, MENU_NTH_ITEM, i+1, 0),
  986.                         MENU_INACTIVE, (i < totalpages ? FALSE : TRUE),
  987.                         0);
  988.     }
  989. }
  990.  
  991.  
  992.                         /*
  993.                          * Now the action procs called after a menu selection
  994.                          * is made.
  995.                          */
  996. /*-->MenuRestart*/
  997.  
  998. void
  999. MenuRestart(object, item)
  1000. Menu object;
  1001. Menu_item item;
  1002. {
  1003.     window_release_event_lock(canvas); /* "I may be some time" */
  1004.     current_page = 0;
  1005.     RestartDviFile();
  1006.     DoPage(current_page);
  1007. }
  1008.  
  1009.  
  1010. /*-->MenuNewPage*/
  1011.  
  1012. void
  1013. MenuNewPage(object, item)
  1014. Menu object;
  1015. Menu_item item;
  1016. {
  1017.     panel_set(textinput,
  1018.                 PANEL_LABEL_STRING, "Page number:",
  1019.                 PANEL_VALUE, "",
  1020.                 PANEL_SHOW_ITEM, TRUE,
  1021.                 0);
  1022.     panel_set(errorheader,
  1023.                 PANEL_SHOW_ITEM, FALSE,
  1024.                 0);
  1025.     panel_set(errormessage,
  1026.                 PANEL_LABEL_STRING, "",
  1027.                 PANEL_SHOW_ITEM, FALSE,
  1028.                 0);
  1029.     window_fit(errorpanel);
  1030.     window_fit(errorframe);
  1031.     window_set(errorframe,
  1032.                 WIN_X, 100,
  1033.                 WIN_Y, 100,
  1034.                 WIN_SHOW, TRUE,
  1035.                 0);
  1036.     textinput_state = M_NEWPAGE;
  1037. }
  1038.  
  1039.  
  1040. /*-->MenuShortPage*/
  1041.  
  1042. void
  1043. MenuShortPage(object, item)
  1044. Menu object;
  1045. Menu_item item;
  1046. {
  1047.         current_page = (int)menu_get(item, MENU_VALUE, 0);
  1048.         current_page--;                        /* as current_page =0 for page 1 */
  1049.         if (current_page >= totalpages)
  1050.             current_page = totalpages - 1;
  1051.         ProcessPage(current_page);
  1052. }
  1053.  
  1054.  
  1055. /*-->MenuNewFile*/
  1056.  
  1057. void
  1058. MenuNewFile(object, item)
  1059. Menu object;
  1060. Menu_item item;
  1061. {
  1062.     panel_set(textinput,
  1063.                 PANEL_LABEL_STRING, "Filename:",
  1064.                 PANEL_VALUE, "",
  1065.                 PANEL_SHOW_ITEM, TRUE,
  1066.                 0);
  1067.     panel_set(errorheader,
  1068.                 PANEL_SHOW_ITEM, FALSE,
  1069.                 0);
  1070.     panel_set(errormessage,
  1071.                 PANEL_LABEL_STRING, "",
  1072.                 PANEL_SHOW_ITEM, FALSE,
  1073.                 0);
  1074.     window_fit(errorpanel);
  1075.     window_fit(errorframe);
  1076.     window_set(errorframe,
  1077.                 WIN_X, 100,
  1078.                 WIN_Y, 100,
  1079.                 WIN_SHOW, TRUE,
  1080.                 0);
  1081.     textinput_state = M_NEWFILE;
  1082. }
  1083.  
  1084.  
  1085. /*-->MenuCd*/
  1086.  
  1087. void
  1088. MenuCd(object, item)
  1089. Menu object;
  1090. Menu_item item;
  1091. {
  1092.     panel_set(textinput,
  1093.                 PANEL_LABEL_STRING, "Directory:",
  1094.                 PANEL_VALUE, "",
  1095.                 PANEL_SHOW_ITEM, TRUE,
  1096.                 0);
  1097.     panel_set(errorheader,
  1098.                 PANEL_SHOW_ITEM, FALSE,
  1099.                 0);
  1100.     panel_set(errormessage,
  1101.                 PANEL_LABEL_STRING, "",
  1102.                 PANEL_SHOW_ITEM, FALSE,
  1103.                 0);
  1104.     window_fit(errorpanel);
  1105.     window_fit(errorframe);
  1106.     window_set(errorframe,
  1107.                 WIN_X, 100,
  1108.                 WIN_Y, 100,
  1109.                 WIN_SHOW, TRUE,
  1110.                 0);
  1111.     textinput_state = M_CD;
  1112. }
  1113.  
  1114.  
  1115. /*-->MenuTex*/
  1116.  
  1117. void
  1118. MenuTex(object, item)
  1119. Menu object;
  1120. Menu_item item;
  1121. {
  1122.     strcpy(ttyinbuf, "cd ");
  1123.     strcat(ttyinbuf, getwd(dirbuf));
  1124.     strcat(ttyinbuf, "\ntex ");
  1125.     strcat(ttyinbuf, curname);
  1126.     strcat(ttyinbuf, ".tex\n");
  1127.     (void)ttysw_input(ttysubwindow, ttyinbuf, strlen(ttyinbuf));
  1128.     main_state |= SB_TEXING;
  1129.     window_set(ttyframe, WIN_SHOW, TRUE, 0);
  1130. }
  1131.  
  1132.  
  1133. /*-->MenuLatex*/
  1134.  
  1135. void
  1136. MenuLatex(object, item)
  1137. Menu object;
  1138. Menu_item item;
  1139. {
  1140.     strcpy(ttyinbuf, "cd ");
  1141.     strcat(ttyinbuf, getwd(dirbuf));
  1142.     strcat(ttyinbuf, "\nlatex ");
  1143.     strcat(ttyinbuf, curname);
  1144.     strcat(ttyinbuf, ".tex\n");
  1145.     (void)ttysw_input(ttysubwindow, ttyinbuf, strlen(ttyinbuf));
  1146.     main_state |= SB_TEXING;
  1147.     window_set(ttyframe, WIN_SHOW, TRUE, 0);
  1148. }
  1149.  
  1150.  
  1151. /*-->MenuEdit*/
  1152.  
  1153. void
  1154. MenuEdit(object, item)
  1155. Menu object;
  1156. Menu_item item;
  1157. {
  1158.     char *tcp;
  1159.  
  1160.     if ((tcp = getenv("EDITOR")) == NULL)
  1161.         tcp = "vi";
  1162.     strcpy(ttyinbuf, tcp);
  1163.     strcat(ttyinbuf, " ");
  1164.     strcat(ttyinbuf, getwd(dirbuf));
  1165.     strcat(ttyinbuf, "/");
  1166.     strcat(ttyinbuf, curname);
  1167.     strcat(ttyinbuf, ".tex\n");
  1168.     (void)ttysw_input(ttysubwindow, ttyinbuf, strlen(ttyinbuf));
  1169.     main_state |= SB_EDITING;
  1170.     window_set(ttyframe, WIN_SHOW, TRUE, 0);
  1171. }
  1172.  
  1173.  
  1174. /*-->MenuShell*/
  1175.  
  1176. void
  1177. MenuShell(object, item)
  1178. Menu object;
  1179. Menu_item item;
  1180. {
  1181.     main_state |= SB_SHELL;
  1182.     window_set(ttyframe, WIN_SHOW, TRUE, 0);
  1183. }
  1184.  
  1185.  
  1186. /*-->MenuToggleFlag*/
  1187.  
  1188. void
  1189. MenuToggleFlag(object, item)
  1190. Menu object;
  1191. Menu_item item;
  1192. {
  1193.     int itemvalue;
  1194.  
  1195.     itemvalue = (int)menu_get(item, MENU_VALUE, 0);
  1196.  
  1197.     switch (itemvalue) {
  1198. #ifdef DEBUG
  1199.         case DM_TOGGLE_D:
  1200.             Debug ^= 1;
  1201.             break;
  1202.         case DM_TOGGLE_X:
  1203.             ExtraDebug ^= 1;
  1204.             break;
  1205. #endif
  1206.         case SM_TOGGLE_P:
  1207.             PreLoad ^= 1;
  1208.             break;
  1209.         case SM_TOGGLE_B:
  1210.             Batching ^= 1;
  1211.             break;
  1212.         case SM_TOGGLE_BIGP:
  1213.             BigPreLoad ^= 1;
  1214.             break;
  1215. #ifdef USEGLOBALMAG
  1216.         case SM_TOGGLE_M:
  1217.             Mflag ^= 1;
  1218.             if (Mflag) {
  1219.                 panel_set(textinput,
  1220.                         PANEL_LABEL_STRING, "Magnification:",
  1221.                         PANEL_VALUE, "",
  1222.                         PANEL_SHOW_ITEM, TRUE,
  1223.                         0);
  1224.                 panel_set(errorheader,
  1225.                         PANEL_SHOW_ITEM, FALSE,
  1226.                         0);
  1227.                 panel_set(errormessage,
  1228.                         PANEL_LABEL_STRING, "",
  1229.                         PANEL_SHOW_ITEM, FALSE,
  1230.                         0);
  1231.                 window_fit(errorpanel);
  1232.                 window_fit(errorframe);
  1233.                 window_set(errorframe,
  1234.                         WIN_X, 100,
  1235.                         WIN_Y, 100,
  1236.                         WIN_SHOW, TRUE,
  1237.                         0);
  1238.                 textinput_state = SM_TOGGLE_M;
  1239.             }
  1240.             break;
  1241. #endif
  1242.         default:
  1243.             break;
  1244.     }
  1245. }
  1246.  
  1247.  
  1248.                         /*
  1249.                          * The event proc for the panel which contains the
  1250.                          * error message item and the text input item.
  1251.                          * If the panel event is an ascii one it passes it
  1252.                          * on to the text input item. It removes the panel
  1253.                          * on a left mouse button.
  1254.                          */
  1255. /*-->ErrorEventProc*/
  1256.  
  1257. void
  1258. ErrorEventProc(object, event)
  1259. Panel object;
  1260. Event *event;
  1261. {
  1262.     if (event_id(event) <= ASCII_LAST)
  1263.         panel_accept_key(textinput, event);
  1264.     else if (event_id(event) == MS_LEFT)
  1265.         window_set(errorframe,
  1266.                 WIN_SHOW, FALSE,
  1267.                 0);
  1268. }
  1269.  
  1270.  
  1271.                         /*
  1272.                          * The notify proc for the panel text input item.
  1273.                          * This gets things like page numbers and filenames
  1274.                          * from the user.
  1275.                          */
  1276. /*-->TextInputProc*/
  1277.  
  1278. void
  1279. TextInputProc(item, event)
  1280. Panel_item item;
  1281. Event *event;
  1282. {
  1283. #ifdef DEBUG
  1284.     if (Debug)
  1285.         fprintf(stderr,"TextInputProc(): panel_get_value returns %s\n",
  1286.                 (char *)panel_get_value(textinput));
  1287. #endif
  1288.     strcpy(textinputbuffer, (char *)panel_get_value(textinput));
  1289.     window_set(errorframe,
  1290.                 WIN_SHOW, FALSE,
  1291.                 0);
  1292.     if (*textinputbuffer == NULL)
  1293.         return;
  1294.     switch(textinput_state) {
  1295.         case M_NEWPAGE:
  1296.             sscanf(textinputbuffer, "%d", ¤t_page);
  1297.             current_page--;
  1298.             if (current_page >= totalpages)
  1299.                 current_page = totalpages - 1;
  1300.             ProcessPage(current_page);
  1301.             break;
  1302.  
  1303.         case M_NEWFILE:
  1304.             window_release_event_lock(errorframe);
  1305.                                 /* as this may take some time */
  1306.             SplitFilename(ExpandTilde(textinputbuffer));
  1307.                                 /* sets dviname */
  1308.             CloseDviFile();        /* from dvifp */
  1309.             FreeFontStorage();
  1310.             SetBop();            /* clear screen */
  1311.             current_page = 0;
  1312.             main_state &= (~SB_INTERPRET & ~SB_NOFILE);
  1313.             CheckFiles();            /* see what's there */
  1314.             OpenDviFile(dviname);                /* sets dvifp */
  1315.             SetFrameLabel();
  1316.             if (main_state & (SB_NOFILE | SB_NODVIFILE)) /* something wrong */
  1317.                 break;
  1318.             SetPageMenu();
  1319.             main_state &= ~SB_NEWFILE;
  1320.             main_state |= SB_INTERPRET;
  1321.             DoPage(current_page);
  1322.             break;
  1323.  
  1324.         case M_CD:
  1325.             if (chdir(ExpandTilde(textinputbuffer)))
  1326.                 Warning("Couldn't change directory");
  1327.             SetFrameLabel();
  1328.             break;
  1329.  
  1330.         case SM_TOGGLE_M:
  1331.             user_mag = atoi(textinputbuffer);
  1332.             break;
  1333.  
  1334.         default:
  1335.             break;
  1336.     }
  1337. }
  1338.  
  1339.  
  1340.                         /*
  1341.                          * The done proc for the frame with the tty subwindow.
  1342.                          * It removes the frame and adjusts the display if the
  1343.                          * DVI file has changed.
  1344.                          */
  1345. /*-->FrameDoneProc*/
  1346.  
  1347. void
  1348. FrameDoneProc(object)
  1349. Frame object;
  1350. {
  1351.     int i;
  1352.  
  1353.     if (Debug) {
  1354.         fprintf(stderr,
  1355.                 "FrameDoneProc(), object = %X, frame = %X, ttyframe = %X\n",
  1356.                 object, frame, ttyframe);
  1357.         fprintf(stderr,"calling default done proc for object\n");
  1358.     }
  1359.     frame_default_done_proc(object);
  1360.     main_state &= (~SB_TEXING & ~SB_EDITING & ~SB_SHELL);
  1361.     CheckFiles();            /* see if anything changed */
  1362.     if (main_state & SB_NEWFILE) { /* we haven't seen it before */
  1363.         OpenDviFile(dviname);
  1364.         if (main_state & SB_NOFILE)/* can't have NODVIFILE as NEWFILE was set */
  1365.             return;
  1366.         main_state &= ~SB_NEWFILE;
  1367.         main_state |= SB_INTERPRET;
  1368.         DoPage(current_page);
  1369.     }
  1370.     else if (DviFileChanged()) {
  1371.         RestartDviFile();
  1372.         DoPage(current_page);
  1373.     }
  1374. }
  1375.  
  1376.  
  1377.                         /*
  1378.                          * Now some utility procedures.
  1379.                          */
  1380. /*-->SplitFilename*/
  1381.  
  1382. void
  1383. SplitFilename(nameptr)
  1384. char *nameptr;
  1385. {
  1386.     char *tcp, *tcp1;
  1387.     Icon temp_icon;                /* for changing label */
  1388.                                 /* process filename */
  1389.     tcp = rindex(nameptr, '/');
  1390.     if (tcp == NULL) {
  1391.                 curarea[0] = '\0';
  1392.                 tcp = nameptr;
  1393.     }
  1394.     else {
  1395.                 strncpy(curarea, nameptr, tcp-nameptr+1);
  1396.                 curarea[tcp-nameptr+1] = '\0'; /* as strncpy will not do this */
  1397.                 tcp += 1;
  1398.     }
  1399.     tcp1 = index(tcp, '.');
  1400.     if (tcp1 == NULL) {
  1401.                 strcpy(curname, tcp);
  1402.                 curext[0] = '\0';
  1403.     }
  1404.     else {
  1405.                 strncpy(curname, tcp, tcp1-tcp);
  1406.                 strcpy(curext, tcp1);
  1407.     }
  1408.  
  1409.     strcpy(dviname, curarea);
  1410.     strcat(dviname, curname);
  1411.     if (curext[0] == '\0')
  1412.                 strcat(dviname, ".dvi");
  1413.     else
  1414.                 strcat(dviname, curext);
  1415. #ifdef DEBUG
  1416.     if (Debug) {
  1417.                 fprintf(stderr,"curarea = %s\n", curarea);
  1418.                 fprintf(stderr,"curname = %s\nsetting icon label to curname\n",
  1419.                         curname);
  1420.                 fprintf(stderr,"curext = %s\n", curext);
  1421.                 fprintf(stderr,"dviname = %s\n", dviname);
  1422.         }
  1423. #endif
  1424.     temp_icon = (Icon)window_get(frame, FRAME_ICON, 0);
  1425.     icon_set(temp_icon, ICON_LABEL, curname, 0);
  1426.     window_set(frame, FRAME_ICON, temp_icon, 0);
  1427. }
  1428.  
  1429.  
  1430.                         /*
  1431.                          * CheckFiles tries access on the .dvi file,
  1432.                          * and if that does not exist, on the .tex file
  1433.                          * and sets main_state accordingly.
  1434.                          */
  1435. /*-->CheckFiles*/
  1436.  
  1437. void
  1438. CheckFiles()
  1439. {
  1440.     char buf[STRSIZE];
  1441.  
  1442.     if (access(dviname, F_OK | R_OK) == 0) { /* .dvi file present */
  1443.         if (main_state & SB_NODVIFILE) /* first time .dvi file found */
  1444.             main_state |= SB_NEWFILE;
  1445.         main_state &= (~SB_NODVIFILE & ~SB_NOTEXFILE);
  1446.     }
  1447.     else {
  1448.         strcpy(buf, curarea);
  1449.         strcat(buf, curname);
  1450.         strcat(buf, ".tex");
  1451.         if (access(buf, F_OK | R_OK) == 0) { /* no .dvi file but .tex file ok */
  1452.             main_state |= SB_NODVIFILE;
  1453.             main_state &= (~SB_NEWFILE & ~SB_NOTEXFILE);
  1454.         }
  1455.         else {                            /* no .tex file either */
  1456.             main_state |= SB_NODVIFILE | SB_NOTEXFILE;
  1457.             main_state &= ~SB_NEWFILE;
  1458.         }
  1459.     }
  1460.         
  1461. }
  1462.  
  1463.  
  1464. /*-->ExpandTilde*/
  1465.  
  1466. char *
  1467. ExpandTilde(iptr)
  1468. char *iptr;
  1469. {
  1470.     char buff[STRSIZE];
  1471.     char *tptr;
  1472.     struct passwd *passwdptr;
  1473.  
  1474. #ifdef DEBUG
  1475.     if (Debug) fprintf(stderr,"ExpandTilde(%s) - ", iptr);
  1476. #endif
  1477.     if (*iptr == '~') {                /* something to do */
  1478.         strcpy(buff, iptr);
  1479.         tptr = index(buff, '/');
  1480.         if ((strlen(buff) == 1) || ((tptr-buff) == 1))
  1481.                                 /* expand to our $HOME */
  1482.             strcpy(iptr, getenv("HOME"));
  1483.         else {                        /* ~name/... form */
  1484.             if (tptr != NULL) *tptr = 0;
  1485.             if ((passwdptr = getpwnam(&buff[1])) != NULL)
  1486.                 strcpy(iptr, passwdptr->pw_dir);
  1487.             if (tptr != NULL) *tptr = '/';
  1488.          }
  1489.         if (tptr != NULL)
  1490.             strcat(iptr, tptr);        /* complete rest of path if there is any */
  1491.     }
  1492. #ifdef DEBUG
  1493.     if (Debug) fprintf(stderr,"returns %s\n", iptr);
  1494. #endif
  1495.     return (iptr);
  1496. }
  1497.  
  1498.  
  1499.                         /*
  1500.                          * Two error message procedures.
  1501.                          * Warning just displays the message, Error displays
  1502.                          * and then abandons the DVI file and goes to the
  1503.                          * NOFILE state.
  1504.                          */
  1505. /*-->Warning*/
  1506.  
  1507. void
  1508. Warning(fmt, arg1, arg2, arg3, arg4)  /* issue a warning */
  1509. char *fmt;        /* format   */
  1510. char *arg1, *arg2, *arg3, *arg4;        /* arguments */
  1511. {
  1512.     char message[STRSIZE];
  1513.  
  1514.     sprintf(message, fmt, arg1, arg2, arg3, arg4);
  1515.  
  1516.     panel_set(errorheader,
  1517.                 PANEL_LABEL_STRING, "TeXtool Warning:",
  1518.                 PANEL_SHOW_ITEM, TRUE,
  1519.                 0);
  1520.     panel_set(errormessage,
  1521.                 PANEL_LABEL_STRING, message,
  1522.                 PANEL_SHOW_ITEM, TRUE,
  1523.                 0);
  1524.     panel_set(textinput,
  1525.                 PANEL_SHOW_ITEM, FALSE,
  1526.                 0);
  1527.     window_fit(errorpanel);
  1528.     window_fit(errorframe);
  1529.     window_set(errorframe,
  1530.                 WIN_X, 100,
  1531.                 WIN_Y, 100,
  1532.                 WIN_SHOW, TRUE,
  1533.                 0);
  1534. }
  1535.  
  1536.  
  1537. /*-->Fatal*/
  1538.  
  1539. void
  1540. Fatal(fmt, arg1, arg2, arg3, arg4)/* issue a fatal error message */
  1541. char *fmt;        /* format */
  1542. char *arg1, *arg2, *arg3, *arg4;        /* arguments */
  1543.  
  1544. {
  1545.     char message[STRSIZE];
  1546.  
  1547.     sprintf(message, fmt, arg1, arg2, arg3, arg4);
  1548.     panel_set(errorheader,
  1549.                 PANEL_LABEL_STRING, "TeXtool Error:",
  1550.                 PANEL_SHOW_ITEM, TRUE,
  1551.                 0);
  1552.     panel_set(errormessage,
  1553.                 PANEL_LABEL_STRING, message,
  1554.                 PANEL_SHOW_ITEM, TRUE,
  1555.                 0);
  1556.     panel_set(textinput,
  1557.                 PANEL_SHOW_ITEM, FALSE,
  1558.                 0);
  1559.     window_fit(errorpanel);
  1560.     window_fit(errorframe);
  1561.     window_set(errorframe,
  1562.                 WIN_X, 100,
  1563.                 WIN_Y, 100,
  1564.                 WIN_SHOW, TRUE,
  1565.                 0);
  1566.     CloseDviFile();
  1567.     FreeFontStorage();                /* to get to known stable state */
  1568.     main_state |= SB_NOFILE;        /* to stop further processing */
  1569.     main_state &= (~SB_NEWFILE & ~SB_NODVIFILE & ~SB_NOTEXFILE & ~SB_INTERPRET);
  1570.     SplitFilename("<no_file>");
  1571.     SetFrameLabel();
  1572. }
  1573.  
  1574.  
  1575. #ifdef DEBUG
  1576.                         /*
  1577.                          * A procedure to show the fds we are using.
  1578.                          * There was a leak of fds somewhere where ptys were
  1579.                          * not closed when a frame with a tty subwindow in it
  1580.                          * was destroyed and this was used to find it.
  1581.                          * The tty subframe is now not destroyed but just
  1582.                          * undisplayed to cure the problem.
  1583.                          * It also shows the huge number of fds needed by
  1584.                          * SunView which cuts down on the number available for
  1585.                          * PXL file caching.
  1586.                          */
  1587. /*-->ShowFd*/
  1588.  
  1589. void
  1590. ShowFd(object, item)
  1591. Menu object;
  1592. Menu_item item;
  1593. {
  1594.     short i;
  1595.     struct stat statbuf;
  1596.  
  1597.     fprintf(stderr,"frame                %d, win%d\n",
  1598.     window_get(frame, WIN_FD), window_get(frame, WIN_DEVICE_NUMBER));
  1599.     fprintf(stderr,"canvas                %d, win%d\n",
  1600.     window_get(canvas, WIN_FD), window_get(canvas, WIN_DEVICE_NUMBER));
  1601.     fprintf(stderr,"ttyframe        %d, win%d\n",
  1602.     window_get(ttyframe, WIN_FD), window_get(ttyframe, WIN_DEVICE_NUMBER));
  1603.     fprintf(stderr,"ttysubwindow        %d, win%d\n",
  1604.     window_get(ttysubwindow, WIN_FD),
  1605.                 window_get(ttysubwindow, WIN_DEVICE_NUMBER));
  1606.     fprintf(stderr,"errorframe        %d, win%d\n",
  1607.     window_get(errorframe, WIN_FD), window_get(errorframe, WIN_DEVICE_NUMBER));
  1608.     fprintf(stderr,"errorpanel        %d, win%d\n",
  1609.     window_get(errorpanel, WIN_FD), window_get(errorpanel, WIN_DEVICE_NUMBER));
  1610.     ShowPxlFd();
  1611.  
  1612.     for (i=0; i<30; i++)                /* enumerate fds */
  1613.         if (fstat(i,&statbuf) == 0)
  1614.             fprintf(stderr,"fd %d open, type = %o, inode = %d\n",
  1615.                     i,
  1616.                     statbuf.st_mode & S_IFMT,
  1617.                     statbuf.st_ino);
  1618. }
  1619. #endif
  1620.  
  1621.